package com.jxbeidou.system.dict.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import com.jxbeidou.core.entity.PageResult;
import com.jxbeidou.core.entity.R;
import com.jxbeidou.system.dict.convert.DictDataConvert;
import com.jxbeidou.system.dict.entity.DictDataDO;
import com.jxbeidou.system.dict.service.DictDataService;
import com.jxbeidou.system.dict.query.DictDataQuery;
import com.jxbeidou.system.dict.vo.DictDataVO;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
* 字典数据表
*
* @author 杜晓祥 
* @since 1.0.0 2023-12-14
*/
@RestController
@RequestMapping("/sys/dict-data")
@AllArgsConstructor
@Tag(name = "字典数据接口")
public class DictDataController {
    @Resource
    private DictDataService dictDataService;

    @PostMapping("page")
    @Operation(summary = "分页查询")
    public R<PageResult<DictDataVO>> page(@RequestBody @Valid DictDataQuery query){
        PageResult<DictDataVO> page = dictDataService.page(query);
        return R.success(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "ID查询")
    public R<DictDataVO> get(@PathVariable("id") Long id){
        DictDataDO entity = dictDataService.getById(id);
        return R.success(DictDataConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "新增")
    public R<String> save(@RequestBody DictDataVO vo){
        dictDataService.save(vo);
        return R.saveSuccess();
    }

    @PutMapping
    @Operation(summary = "修改")
    public R<String> update(@RequestBody @Valid DictDataVO vo){
        dictDataService.update(vo);
        return R.modifySuccess();
    }

    @DeleteMapping("{id}")
    @Operation(summary = "ID删除")
    public R<String> delete(@PathVariable("id") Long id){
        dictDataService.delete(Arrays.asList(id));
        return R.deleteSuccess();
    }

    @PutMapping("delete")
    @Operation(summary = "批量删除")
    public R<String> delete(@RequestBody List<Long> idList) {
        dictDataService.delete(idList);
        return R.deleteSuccess();
    }
}